package com.xique.door.util;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class HttpUtil<T> {

    @Resource
    private RestTemplate restTemplate;

    /**
     * get请求，单一访问，不带参数
     *
     * @param url   访问地址
     * @param clazz 得到的结果的返回类型
     * @return 返回的是最终的返回结果
     */
    public T getMethodBody(String url, Class<T> clazz) {
        ResponseEntity<String> exchange = restTemplate.getForEntity(url, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * get请求，带上请求头，不带参数
     *
     * @param url     访问地址
     * @param clazz   最终返回的接口的类型类的反射
     * @param headers 请求头内容，使用Map<String,String>，更容易辨认
     * @return
     */
    public T getMethodBody(String url, Class<T> clazz, Map<String, String> headers) {
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach((key, value) -> {
            requestHeaders.set(key, value);
        });
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * get请求，带上请求头，带参数
     *
     * @param url     访问地址
     * @param clazz   最终返回的接口的类型类的反射
     * @param headers 请求头内容，使用Map<String,String>，更容易辨认
     * @param params  参数，使用Map<String,String>
     * @return
     */
    public T getMethodBody(String url, Map<String, String> params, Class<T> clazz, Map<String, String> headers) {
        if (params == null && headers == null) {
            return this.getMethodBody(url, clazz);
        }
        if (headers == null) {
            return this.getMethodBody(url, clazz);
        }
        if (params == null) {
            return this.getMethodBody(url, clazz);
        }
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach((key, value) -> {
            requestHeaders.set(key, value);
        });
        String paramStr = "?";
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (i != 0) {
                paramStr = paramStr + entry.getKey() + "=" + entry.getValue();
            } else {
                paramStr = "&" + paramStr + entry.getKey() + "=" + entry.getValue();
            }
            i++;
        }
        url += paramStr;
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * get请求，不带上请求头，带参数
     *
     * @param url    访问地址
     * @param params 参数，使用Map<String,String>
     * @param clazz  最终返回的接口的类型类的反射
     * @return
     */
    public T getMethodBody(String url, Map<String, String> params, Class<T> clazz) {
        String paramStr = "?";
        int i = 0;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (i != 0) {
                paramStr = paramStr + entry.getKey() + "=" + entry.getValue();
            } else {
                paramStr = "&" + paramStr + entry.getKey() + "=" + entry.getValue();
            }
            i++;
        }
        url += paramStr;
        ResponseEntity<String> exchange = restTemplate.getForEntity(url, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * post请求，不带任何参数和请求头
     *
     * @param url   请求地址
     * @param clazz 最终返回结果封装的类型
     * @return 放回封装后的放回结果
     */
    public T postMethodBody(String url, Class<T> clazz) {
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, null, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * post请求，带请求头，不带参数
     *
     * @param url     请求地址
     * @param clazz   最终返回结果封装的类型
     * @param headers 请求头 ，用Map<String ,String>
     * @return 放回封装后的放回结果
     */
    public T postMethodBody(String url, Class<T> clazz, Map<String, String> headers) {
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach((key, value) -> {
            requestHeaders.set(key, value);
        });
        HttpEntity<String> requestEntity = new HttpEntity<String>(null, requestHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }

    /**
     * post请求，不带请求头，带参数
     *
     * @param url    请求地址
     * @param clazz  最终返回结果封装的类型
     * @param params 参数 ，用Map<String ,String>
     * @return 放回封装后的放回结果
     */
    public T postMethodBody(String url, Map<String, String> params, Class<T> clazz) {
        JSONObject msg = new JSONObject();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            msg.put(entry.getKey(), entry.getValue());
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(msg.toJSONString(), null);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return JSONObject.parseObject(exchange.getBody(), clazz);
    }


    /**
     * post请求，带请求头，带参数
     *
     * @param url     请求地址
     * @param params  参数 ，用Map<String ,String>
     * @param headers 请求头 ，用Map<String ,String>
     * @return 放回封装后的放回结果
     */
    public ResponseEntity<String> postMethodBody(String url, Map<String, String> params, Map<String, String> headers) {
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach((key, value) -> {
            requestHeaders.set(key, value);
        });
        JSONObject msg = new JSONObject();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            msg.put(entry.getKey(), entry.getValue());
        }
        HttpEntity<String> requestEntity = new HttpEntity<String>(msg.toJSONString(), requestHeaders);
        RestTemplate restTemplate1 = new RestTemplate();
        ResponseEntity<String> exchange = restTemplate1.exchange(url, HttpMethod.POST, requestEntity, String.class);
        return exchange;
    }


    public static void main(String[] args) {

        HashMap<String, String> map1 = new HashMap<>();
        map1.put("name","test1234");
        map1.put("phone","15064120232");
        map1.put("tag","测试");
        map1.put("cardNo","123456");
        map1.put("idCardNo","330232132131311412");
        map1.put("password","123456");
        map1.put("addition","");

        String path="/v2/face/register";
        HttpUtil objectHttpUtil = new HttpUtil();
        Map<String, String> map = new HashMap<>();
        map.put("token", "cc16a276817324ef330af7965c1752c49aa6270c2967b9da9168f520398fd94d");
        map.put("projectGuid", "0115B43CE19B4C10969EF76BF0AC6BA6");
        map.put("Content-Type","application/json");
        ResponseEntity responseEntity = objectHttpUtil.postMethodBody("http://wo-api.uni-ubi.com" + path, map1, map);
        System.out.println(responseEntity);
    }


}